﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class Modules_System_RoleAdd : System.Web.UI.Page
{
    ISys_RoleHandler roleHandler = new Sys_RoleManage();
    ISys_ResourceHandler resourceHandler = new Sys_ResourceManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private void dataBind()
    {
        DataTable dtStatus = new DataTable();
        dtStatus.Columns.Add("Value");
        dtStatus.Columns.Add("Name");
        DataRow drStatus;

        drStatus = dtStatus.NewRow();
        drStatus[0] = "1";
        drStatus[1] = Enums.GetNormalStatusName(Enums.NormalStatus.Vallid);
        dtStatus.Rows.Add(drStatus);

        drStatus = dtStatus.NewRow();
        drStatus[0] = "0";
        drStatus[1] = Enums.GetNormalStatusName(Enums.NormalStatus.Invalid);
        dtStatus.Rows.Add(drStatus);

        ddlStatus.DataSource = dtStatus;
        ddlStatus.DataTextField = "Name";
        ddlStatus.DataValueField = "Value";
        ddlStatus.DataBind();

        string rolePur = "";
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                Sys_RoleQuery query = roleHandler.GetSys_RoleQuery(ID);
                if (query != null)
                {
                    txtCode.Text = query.Code;
                    txtName.Text = query.Name;
                    txtRemark.Text = query.Remark;
                    rolePur = query.Purviews;

                    ddlStatus.SelectedValue = query.Status.ToString();
                }
            }
        }
        hidnPurviews.Value = rolePur;

        string strPur = Session["UserPurviews"].ToString();
        Sys_ResourceQuery[] list = resourceHandler.GetSys_ResourceQueryList(" 1=1 ", new SqlParameter[]{}, " ID asc");
        StringBuilder builder = new StringBuilder();
        int first = 0;
        foreach (Sys_ResourceQuery firstQuery in list)
        {
            if (firstQuery.ID >= (first + 10000))
            {
                first = firstQuery.ID;

                builder.Append("<li><input type=\"checkbox\" name=\"resid\" value='" + firstQuery.ID + "' " + (rolePur.Contains(firstQuery.ID.ToString()) ? "checked=\"checked\"" : "") + "/><label>" + firstQuery.Name + "</label>");
                bool hadSecondChild = false;
                foreach (Sys_ResourceQuery secondQuery in list)
                {
                    int secTmp = secondQuery.ID - firstQuery.ID;
                    if (secondQuery.ID == firstQuery.ID)
                        continue;

                    if (secTmp < 10000 && (secTmp % 1000) == 0)
                    {
                        if(secTmp >= 0){
                            if (!hadSecondChild)
                            {
                                builder.Append("<ul>");
                                hadSecondChild = true;
                            }

                            builder.Append("<li><input type=\"checkbox\" name=\"resid\" value='" + secondQuery.ID + "' " + (rolePur.Contains(secondQuery.ID.ToString()) ? "checked=\"checked\"" : "") + "/><label>" + secondQuery.Name + "</label>");

                            bool hadThirdChild = false;
                            foreach (Sys_ResourceQuery thirdQuery in list)
                            {
                                if (thirdQuery.ID == secondQuery.ID || thirdQuery.ID == firstQuery.ID)
                                    continue;
                                int thdTmp = thirdQuery.ID - secondQuery.ID;
                                if (thdTmp < 100)
                                {
                                    if (thdTmp >= 0)
                                    {
                                        if (!hadThirdChild)
                                        {
                                            builder.Append("<ul>");
                                            hadThirdChild = true;
                                        }

                                        builder.Append("<li><input type=\"checkbox\" name=\"resid\" value='" + thirdQuery.ID + "' " + (rolePur.Contains(thirdQuery.ID.ToString()) ? "checked=\"checked\"" : "") + "/><label>" + thirdQuery.Name + "</label></li>");
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (hadThirdChild)
                                builder.Append("</ul>");
                            builder.Append("</li>");
                        }
                    }
                    else if(secTmp >= 10000)
                    {
                        break;
                    }
                }
                if (hadSecondChild)
                    builder.Append("</ul>");
                builder.Append("</li>");
            }
        }
        treeContainer.InnerHtml = builder.ToString();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ("".Equals(txtCode.Text.Trim()))
        {
            Jscript.Alert("代码不能为空", this.Page);
            return;
        }
        if ("".Equals(txtName.Text.Trim()))
        {
            Jscript.Alert("名称不能为空", this.Page);
            return;
        }
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (isCodeExists(txtCode.Text.Trim(), int.Parse(Request["ID"])))
            {
                Jscript.Alert("代码已存在", this.Page);
                return;
            }
        }
        else
        {
            if (isCodeExists(txtCode.Text.Trim()))
            {
                Jscript.Alert("代码已存在", this.Page);
                return;
            }
        }

        Sys_RoleQuery query = new Sys_RoleQuery();
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            query = roleHandler.GetSys_RoleQuery(int.Parse(Request["ID"]));
        }
        query.Code = txtCode.Text.Trim();
        query.Name = txtName.Text.Trim();
        query.Status = int.Parse(ddlStatus.SelectedValue.ToString());
        query.Purviews = hidnPurviews.Value;
        query.Remark = txtRemark.Text;

        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (roleHandler.UpdateSys_Role(query))
            {
                Jscript.Alert("更新成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
        else
        {
            if (roleHandler.InsertSys_Role(query))
            {
                Jscript.Alert("添加成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
    }


    private bool isCodeExists(string code)
    {
        bool istrue = false;

        DataTable dt = roleHandler.GetSys_RoleList(" Code = @Code", new SqlParameter[] { new SqlParameter("@Code", code) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    private bool isCodeExists(string code, int postId)
    {
        bool istrue = false;

        DataTable dt = roleHandler.GetSys_RoleList("Code=@Code and ID!=@Id", new SqlParameter[] { new SqlParameter("@Code", code), new SqlParameter("@Id", postId.ToString()) });

        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

}
